﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Region = Website.Models.Domain.Region
@{
    this.ViewBag.Title = "Matchmaking";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Regions = this.ViewData["Regions"] as List<Region>;
    var Funds = this.ViewData["Funds"] as List<Fund>;
    var PartnerTypes = this.ViewData["PartnerTypes"] as List<MatchmakingPartnerType>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var Profiles = this.ViewData["Profiles"] as Website.Models.ViewModels.ProfilesView;

    // errorhandling
    var errorMsg = new HtmlString(string.Empty);
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

}
<script type="text/javascript">
    //toggle script
    function toggle(id) {
        var allFields = new Array('NotStarted', 'Started', 'Completed');
        for (var i = 0; i < allFields.length; i++) {
            document.getElementById(allFields[i]).style.display = 'none';
        }

        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }

    function toggleRD(id) {
        var allFields = new Array('RAndD', 'NotRAndD');
        for (var i = 0; i < allFields.length; i++) {
            document.getElementById(allFields[i]).style.display = 'none';
        }

        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>
<script>
    $(function() {
        $("#datepicker").datepicker();
        $('#OwnerProjects').multiSelect();
    });
</script>

@using (this.Html.BeginForm())
{
    <div class="error">
        @errorMsg
    </div>

    // ********************Matchmaking*********************

    // name
    <div class="formBullet">Navn</div>
    <input type="text" id="Name" name="Name" />

    // date
    <div class="formBullet">Dato</div>
    <input type="text" id="datepicker" name="Date" readonly="readonly" value="" />

    // location
    <div class="formBullet">Lokation</div>
    <div class="formEntry">
        <select name="Location" id="Location">
            <option value="@Guid.Empty">Vælg en lokation</option>
            @foreach (Location loc in Profiles.Locations.OrderBy(x => x.Name))
            {
                <option value="@loc.ID">@loc.Name</option>
            }
        </select>
        <input type="button" value="Ny" id="newLocation" />
    </div>

    // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet (inkl. hoved projekt)</div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects)
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

    // buyers
    <div class="formBullet">Købere</div>
    <input type="text" id="Buyers" name="Buyers" />

    // funds chooser 
    <div class="formBullet">Puljer og fonde</div>
    <div class="formEntry">
        <select multiple="multiple" name="Funds" id="Funds">
            @foreach (Fund prof in Funds)
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>

    // funds comments
    <div class="formBullet">Uddybning ang. fonde</div>
    <textarea id="FundsText" name="FundsText"></textarea>

    // funds chooser 
    <div class="formBullet">Anden Finansiering</div>
    <div class="formEntry">
        <select multiple="multiple" name="OtherFinancing" id="OtherFinancing">
            @foreach (Fund prof in Funds)
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>

    <hr />

    // Matchmaking Partner
    <div class="formBullet">Samarbejdspartner</div>
    <div class="formEntry">
        <select name="Type" id="Type">
            <option value="@Guid.Empty">Vælg en samarbejdspartner</option>
            @foreach (MatchmakingPartnerType pt in PartnerTypes)
            {
                <option value="@pt.ID">@pt.Name</option>
            }
        </select>
    </div>

    // PD comments
    <div class="formBullet">Produkt udvikling</div>
    <textarea id="ProductDevelopmentText" name="ProductDevelopmentText"></textarea>

    // test comments
    <div class="formBullet">Test</div>
    <textarea id="TestText" name="TestText"></textarea>

    // sales comments
    <div class="formBullet">Salg</div>
    <textarea id="SalesText" name="SalesText"></textarea>

    // other comments
    <div class="formBullet">Andet</div>
    <textarea id="OtherText" name="OtherText"></textarea>
    <hr />

    // Other
    <div class="formBullet">Andet til matchmaking</div>
    <textarea id="Comments" name="Comments"></textarea>

    // ********************Submit*********************
    <div class="buttonHolder">
        <input type="submit" value="Opret" />
    </div>
}

<script>

    @*
        Og nu da det er rykket herned kan du bruge @otherID til at lave dit script.
        Alt hvad der bliver skrevet med et @ bliver parset (server-side) før siden er endeligt loadet.
    *@
    $(function() {
        $('#Funds').multiSelect();
        $('#OtherFinancing').multiSelect();

                                                        @*        //choosing "andet" will pop up an additional text-box for input on the subject

        $('#Presentations').multiSelect({
            afterSelect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            },

            afterDeselect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            }
        });*@
        //handle locations
        $("#newLocation").click(function() {
            $("#dialog").dialog({
                width: "340px",
                modal: true
            });
        });
        $('#CreateLocationForm').submit(function(e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function(result) {
                    if (result.success) {
                        // We have a JSON object in case of success
                        $('#Locations')
                            .append($("<option></option>")
                                .attr("value", result.locationID)
                                .text(result.locationName))
                            .val(result.locationID);


                        $("#dialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#CreateLocationForm').html(result);
                    }
                }
            });
        });

    });
</script>

<div id="dialog" title="Opret ny lokation" style="display: none;">
    @Html.Partial("~/Views/Locations/_CreateLocationPartial.cshtml", new Location())
</div>